package com.liyang.springcloud.controller;

import com.liyang.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @BelongsProject: cloud-2024
 * @BelongsPackage: com.liyang.springcloud.controller
 * @Author: liyang
 * @CreateTime: 2023-12-27  15:42
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/consumer/hystrix")
@DefaultProperties(defaultFallback = "globalFallbackHandler")
public class PaymentController {

    @Resource
    private PaymentHystrixService paymentService;

    @Value("${server.port}")
    private String serverPort;


    /**
     * 正常访问
     *
     * @param id
     * @return
     */
    @GetMapping("/ok/{id}")
    public String paymentInfoOK(@PathVariable("id") Integer id) {
        String result = paymentService.paymentInfoOK(id);
        log.info("result: " + result);
        return result;
    }


    /**
     * 超时或者异常
     *
     * @param id
     * @return
     */
    @GetMapping("/timeout/{id}")
    @HystrixCommand
//    @HystrixCommand(fallbackMethod = "paymentInfoTimeOutHandler", commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
//    })
    public String paymentInfoTimeOut(@PathVariable("id") Integer id) {

        // 自生出错
//        int age = 10/0;

        String result = paymentService.paymentInfoTimeOut(id);
        log.info("result: " + result);
        return result;
    }

    public String paymentInfoTimeOutHandler(Integer id) {
        return "我是消费者80,对方支付系统系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";
    }

    /**
     * @description: 全局降级方法
     * @author: liyang
     * @date: 2023/12/28 14:13
     * @param: id
     * @return: java.lang.String
     **/
    public String globalFallbackHandler() {
        return "全局降级方法";
    }
}
